package me.xlgp.xiquzimu.service;

import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;

import me.xlgp.xiquzimu.listener.OnPingLunListener;
import me.xlgp.xiquzimu.util.PingLunHelper;

public class PingLunService {

    //标记是否按当前唱词间隔时间
    public static Integer CURRENT_MILLIS = 0;

    private final Handler handler;

    private Runnable runnable;

    private long startTime;

    public PingLunService() {
        handler = new Handler(Looper.getMainLooper());
    }

    public void start(long delayMillis) {
        handler.removeCallbacks(runnable);
        runnable = new PinglunRunnable();
        startTime = SystemClock.uptimeMillis();
        handler.postDelayed(runnable, delayMillis);
    }

    public int getAtTime() {
        return (int) (SystemClock.uptimeMillis() - startTime);
    }

    public void run(CharSequence content, OnPingLunListener onPingLunListener) {//此时评论数据框已打开；开始发送字幕，包括输入内容，再点击发送按钮
        PingLunHelper.input(DouYinAccessibilityService.getInstance(), content, onPingLunListener);
    }

    public void disable() {
        handler.removeCallbacks(runnable);
    }

    static class PinglunRunnable implements Runnable {

        @Override
        public void run() {
            try {
                //打开数输入框
                PingLunHelper.openInputLayout(DouYinAccessibilityService.getInstance());
            } catch (Exception e) {
                Log.e("TAG", "run: ", e);
            }
        }
    }
}
